package com.tg.appcommon.android;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;
import android.util.Log;

/**
 * 重定向
 */
public class TGResources {

    private static final String SP_FILE_NAME = "TAN_GE_UI_CUSTOMIZE";
    private static final String SP_KAY_PREFIX_LAYOUT = "SP_KAY_PREFIX_LAYOUT_";
    private static final String SP_KAY_PREFIX_LAYOUT_ELEMENT = "SP_KAY_PREFIX_LAYOUT_ELEMENT_";
    private static final String SP_ENABLE_SWITCH = "TAN_GE_UI_CUSTOMIZE_SWITCH";
    private static final String TAG = "TGResources";
    private static boolean REDIRECT = false;

    public static boolean isRedirectEnabled() {
        return REDIRECT;
    }

    public static void setRedirectEnabled(boolean enable) {
        REDIRECT = enable;
    }

    /**
     * Debug模式 + SP_ENABLE_SWITCH打开 的情况 下，
     * 若通过调试页面选择了其他布局，
     * 则会替换成对应的布局文件，如 activity_login.xml -> activity_login_background.xml
     * 如果找不到对应的布局文件，那么用回参数对应的默认布局。
     *
     * @param context
     * @param originResourceId
     * @return
     */
    public static int redirectLayout(Context context, int originResourceId, String pageType) {

        if (REDIRECT) {

            if (!readUiCustomizeSwitch(context)) {
                return originResourceId;
            }

            String resourceEntryName = "";
            String resourcePackageName = "";
            try {
                resourceEntryName = context.getResources().getResourceEntryName(originResourceId);
                resourcePackageName = context.getResources().getResourcePackageName(originResourceId);
            } catch (Throwable ex) {
                Log.e(TAG, "[redirectLayout] failed to find entry name: " + ex.toString());
                // 找不到对应的资源名？返回默认ID
                return originResourceId;
            }
            if (resourceEntryName == null || resourceEntryName.length() <= 0) {
                // 找不到对应的资源名？返回默认ID
                return originResourceId;
            }

            String customizeConfig = readRedirectConfig(context, pageType);

            if (!TextUtils.isEmpty(customizeConfig)) {

                String targetResourceEntryName = resourceEntryName + "_" + customizeConfig;
                int targetResourceId = 0;
                try {
                    targetResourceId = context.getResources().getIdentifier(targetResourceEntryName, "layout", resourcePackageName);
                } catch (Throwable ex) {
                    Log.e(TAG, "[redirectLayout] redirect error: " + ex.toString());
                    return originResourceId;
                }

                if (targetResourceId != 0) {
                    return targetResourceId;
                } else {
                    Log.e(TAG, "[redirectLayout] redirect error: can NOT found targetResourceId for " + targetResourceEntryName);
                    return originResourceId;
                }
            } else {
                // 没有手动设置样式？返回默认ID
                return originResourceId;
            }
        }

        return originResourceId;
    }

    public static void setUiCustomizeSwitch(Context context, boolean enable) {
        if (context == null) {
            throw new IllegalArgumentException("context == null");
        }

        SharedPreferences sharedPreferences = context.getSharedPreferences(SP_FILE_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putBoolean(SP_ENABLE_SWITCH, enable);
        editor.commit();
    }

    public static boolean readUiCustomizeSwitch(Context context) {
        if (context == null) {
            throw new IllegalArgumentException("context == null");
        }

        SharedPreferences sharedPreferences = context.getSharedPreferences(SP_FILE_NAME, Context.MODE_PRIVATE);
        return sharedPreferences.getBoolean(SP_ENABLE_SWITCH, false);
    }

    public static String readRedirectConfig(Context context, String pageType) {
        if (context == null) {
            throw new IllegalArgumentException("context == null");
        }
        if (pageType == null || pageType.length() <= 0) {
            throw new IllegalArgumentException("resourceName empty");
        }

        SharedPreferences sharedPreferences = context.getSharedPreferences(SP_FILE_NAME, Context.MODE_PRIVATE);
        String selectedStyleIndex = sharedPreferences.getString(SP_KAY_PREFIX_LAYOUT + pageType, "");
        return selectedStyleIndex;
    }

    public static void writeRedirectConfig(Context context, String pageType, String selectedStyleIndex) {
        if (context == null) {
            throw new IllegalArgumentException("context == null");
        }
        if (pageType == null || pageType.length() <= 0) {
            throw new IllegalArgumentException("resourceName empty");
        }

        SharedPreferences sharedPreferences = context.getSharedPreferences(SP_FILE_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString(SP_KAY_PREFIX_LAYOUT + pageType, selectedStyleIndex);
        editor.commit();
    }

    public static int readLoginButtonStyle(Context context) {
        if (context == null) {
            throw new IllegalArgumentException("context == null");
        }

        SharedPreferences sharedPreferences = context.getSharedPreferences(SP_FILE_NAME, Context.MODE_PRIVATE);
        int selectedStyleIndex = sharedPreferences.getInt(SP_KAY_PREFIX_LAYOUT_ELEMENT + "login_button", 0);
        return selectedStyleIndex;
    }

    public static void writeLoginButtonStyle(Context context, int selectedStyleIndex) {
        if (context == null) {
            throw new IllegalArgumentException("context == null");
        }

        SharedPreferences sharedPreferences = context.getSharedPreferences(SP_FILE_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putInt(SP_KAY_PREFIX_LAYOUT_ELEMENT + "login_button", selectedStyleIndex);
        editor.commit();
    }

    public static int readLoginWechatButtonStyle(Context context) {
        if (context == null) {
            throw new IllegalArgumentException("context == null");
        }

        SharedPreferences sharedPreferences = context.getSharedPreferences(SP_FILE_NAME, Context.MODE_PRIVATE);
        int selectedStyleIndex = sharedPreferences.getInt(SP_KAY_PREFIX_LAYOUT_ELEMENT + "login_wechat_button", 0);
        return selectedStyleIndex;
    }

    public static void writeLoginWechatButtonStyle(Context context, int selectedStyleIndex) {
        if (context == null) {
            throw new IllegalArgumentException("context == null");
        }

        SharedPreferences sharedPreferences = context.getSharedPreferences(SP_FILE_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putInt(SP_KAY_PREFIX_LAYOUT_ELEMENT + "login_wechat_button", selectedStyleIndex);
        editor.commit();
    }

    public static int readLoginEditTextStyle(Context context) {
        if (context == null) {
            throw new IllegalArgumentException("context == null");
        }

        SharedPreferences sharedPreferences = context.getSharedPreferences(SP_FILE_NAME, Context.MODE_PRIVATE);
        int selectedStyleIndex = sharedPreferences.getInt(SP_KAY_PREFIX_LAYOUT_ELEMENT + "login_edit_text", 0);
        return selectedStyleIndex;
    }

    public static void writeLoginEditTextButtonStyle(Context context, int selectedStyleIndex) {
        if (context == null) {
            throw new IllegalArgumentException("context == null");
        }

        SharedPreferences sharedPreferences = context.getSharedPreferences(SP_FILE_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putInt(SP_KAY_PREFIX_LAYOUT_ELEMENT + "login_edit_text", selectedStyleIndex);
        editor.commit();
    }

    public static int readHomePageStyle(Context context) {
        if (context == null) {
            throw new IllegalArgumentException("context == null");
        }

        SharedPreferences sharedPreferences = context.getSharedPreferences(SP_FILE_NAME, Context.MODE_PRIVATE);
        int selectedStyleIndex = sharedPreferences.getInt(SP_KAY_PREFIX_LAYOUT_ELEMENT + "_home_page", 1);
        return selectedStyleIndex;
    }

    public static void writeHomePageStyle(Context context, int selectedStyleIndex) {
        if (context == null) {
            throw new IllegalArgumentException("context == null");
        }

        SharedPreferences sharedPreferences = context.getSharedPreferences(SP_FILE_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putInt(SP_KAY_PREFIX_LAYOUT_ELEMENT + "_home_page", selectedStyleIndex);
        editor.commit();
    }

    public static int readDeviceListPageStyle(Context context) {
        if (context == null) {
            throw new IllegalArgumentException("context == null");
        }

        SharedPreferences sharedPreferences = context.getSharedPreferences(SP_FILE_NAME, Context.MODE_PRIVATE);
        int selectedStyleIndex = sharedPreferences.getInt(SP_KAY_PREFIX_LAYOUT_ELEMENT + "_device_list_page", 0);
        return selectedStyleIndex;
    }

    public static void writeDeviceListPageStyle(Context context, int selectedStyleIndex) {
        if (context == null) {
            throw new IllegalArgumentException("context == null");
        }

        SharedPreferences sharedPreferences = context.getSharedPreferences(SP_FILE_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putInt(SP_KAY_PREFIX_LAYOUT_ELEMENT + "_device_list_page", selectedStyleIndex);
        editor.commit();
    }

    public static int readMinePageStyle(Context context) {
        if (context == null) {
            throw new IllegalArgumentException("context == null");
        }

        SharedPreferences sharedPreferences = context.getSharedPreferences(SP_FILE_NAME, Context.MODE_PRIVATE);
        int selectedStyleIndex = sharedPreferences.getInt(SP_KAY_PREFIX_LAYOUT_ELEMENT + "_mine_page", 0);
        return selectedStyleIndex;
    }

    public static void writeMinePageStyle(Context context, int selectedStyleIndex) {
        if (context == null) {
            throw new IllegalArgumentException("context == null");
        }

        SharedPreferences sharedPreferences = context.getSharedPreferences(SP_FILE_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putInt(SP_KAY_PREFIX_LAYOUT_ELEMENT + "_mine_page", selectedStyleIndex);
        editor.commit();
    }

}
